//  Double Sum 3

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    --a[i];
  }
  vector<int> lst(n + 1, -1), nxt(n + 1, n);
  vector<int> l(n, -1), r(n, n);
  for (int i = 0; i < n; i++) {
    l[i] = lst[a[i] + 1];
    lst[a[i]] = i;
  }
  for (int i = n - 1; i >= 0; i--) {
    r[i] = min(nxt[a[i] + 1], nxt[a[i]]);
    nxt[a[i]] = i;
  }
  i64 ans = 0;
  for (int i = 0; i < n; i++) {
    //		cerr << i << ' ' << l[i] << ' ' << r[i] << endl;
    ans += 1ll * (i - l[i]) * (r[i] - i);
  }
  cout << ans << endl;
  return 0;
}
